package com.hyx.business.controller.admin;

import com.hyx.server.dto.CategoryDto;
import com.hyx.server.dto.ResponseDto;
import com.hyx.server.service.CategoryService;
import com.hyx.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    public static final String BUSINESS_NAME = "分类";

    @Resource
    private CategoryService categoryService;

    /**
     * 列表查询
     * @return
     */
    @PostMapping("/all")
    public ResponseDto all(){
        ResponseDto responseDto = new ResponseDto();
        List<CategoryDto> categoryDtoList = categoryService.Alllist();
        responseDto.setContent(categoryDtoList);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     * @param categoryDto
     * @return
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody CategoryDto categoryDto){

            ValidatorUtil.require(categoryDto.getParent(),"课程ID");
            ValidatorUtil.require(categoryDto.getName(),"名称");
            ValidatorUtil.length(categoryDto.getName(),"名称",1,58);

        ResponseDto responseDto = new ResponseDto();
        categoryService.save(categoryDto);
        responseDto.setContent(categoryDto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        categoryService.delete(id);
        return responseDto;
    }

}